<?php
	Header('Cache-Control: no-cache');
	Header('Pragma: no-cache');
	Header('Content-type: text/javascript');
	
	session_start();

	require_once '../database/database.class.php';
	$db = new Database();
	
	// Javascript Variablen erzeugen
	// Einstellungen
	echo $db->jsMapSettings();
?>
var whereaboutsMap = {};	// Aufenthaltsorte
var districtsMap = {};		// Revierankerpunkte
var hunterDistricts = {};	// Reviere eines Jägers
var map = null;		// Google maps map :)
var hunterID = 0;	// ID des gewälhlten Jägers
var districtID = 0;	// ID des gewählten Reviers
var typeToDraw = 1;	// ID des gewählten Orttyps
var markers = [];	// Eingefügte, neue Marker
var toDelete = [];	// Marker, die zum Löschen markiert wurden
var whereaboutDrawings = [];	// Marker aus der Datenbank
var districtDrawings = [];	// Revierzeichnungen
var markerIcon = null;	// Icon für die Marker
var opMode = -1;	// Operationsmodus
					// für Whereabouts:
					// -1: undefiniert/nicht ausgewählt
					// 0: Orte speichern
					// 1: Orte löschen
					// für Reviere:
					// -1: undefiniert/nicht ausgewählt
					// 0: Revier neu abstecken
var editMode = -1;	// Aktueller Kartenmodus:
					// -1: undefiniert/nicht ausgewählt
					// 0: Orte bearbeiten (whereabouts.php)
					// 1: Reviere bearbeiten (districts.php)
					// 2: Karte mit allen Revieren und Orten anzeigen
					// (index.php)
var ajaxSuccess = true;	// Gibt an, ob eine AJAX Operation erfolgreich war
var displayedWhereaboutTypes = [];	// Angezeigte Ortstypen (nur index.php)
var displayedDistricts = [];	// Angezeigte Reviere (nur index.php)

function drawWhereabout(center, icon, timestamp, hunter, id) {
	
	var markerOptions = {
		clickable: true,
		map: map,
		position: center,
		icon: icon,
		title: timestamp + "\n" + hunter
	};

	var marker = new google.maps.Marker(markerOptions);
	
	if (editMode == 0) {
		google.maps.event.addListener(marker, 'click', function() {
			if (opMode == 1) {
				toDelete.push(id);
				this.setMap(null);
			}
		});
	}

	whereaboutDrawings.push(marker);
}

function drawWhereaboutById() {
	deleteWhereaboutDrawings();
	deleteMarkers();

	for (var whereabout in whereaboutsMap) {
		// Punkt hinzufuegen
		drawWhereabout(whereaboutsMap[whereabout].center,
						whereaboutsMap[whereabout].icon,
						whereaboutsMap[whereabout].timestamp,
						whereaboutsMap[whereabout].hunter,
						whereabout);
    }
}

function drawAllWhereabouts(init) {
	deleteWhereaboutDrawings();

	var marker;
	var showSince = 
		document.showSince.year.options[document.showSince.year.selectedIndex].text + 
		"-" + 
		document.showSince.month.options[document.showSince.month.selectedIndex].text + 
		"-" + 
		document.showSince.day.options[document.showSince.day.selectedIndex].text + 
		" 00:00:00";
	var showUntil = 
		document.showUntil.year.options[document.showUntil.year.selectedIndex].text + 
		"-" + 
		document.showUntil.month.options[document.showUntil.month.selectedIndex].text + 
		"-" + 
		document.showUntil.day.options[document.showUntil.day.selectedIndex].text + 
		" 23:59:59";

	for (var whereabout in whereaboutsMap) {
		if (init) {
			displayedWhereaboutTypes[whereaboutsMap[whereabout].typeid] = true;
		}
	 
		if (whereaboutsMap[whereabout].compareTime >= showSince && 
			whereaboutsMap[whereabout].compareTime <= showUntil &&
			displayedWhereaboutTypes[whereaboutsMap[whereabout].typeid] &&
			displayedDistricts[whereaboutsMap[whereabout].districtID]) { 
			
			var markerOptions = {
				clickable: true, 
				map: map, 
				position: whereaboutsMap[whereabout].center,
				icon: whereaboutsMap[whereabout].icon, 
				title: whereaboutsMap[whereabout].timestamp + "\n" + whereaboutsMap[whereabout].hunter 
			};
		 
		 whereaboutDrawings.push( new google.maps.Marker(markerOptions) ); 
		 } 
	}
}

function deleteDistrictDrawing() {	
	for (var i = 0; i < districtDrawings.length; i++ ) {
		districtDrawings[i].setMap(null);
	}
	districtDrawings = [];
}

function drawDistrict(lineCoordinates, color) {
	if (editMode != 2) {
		// Die alte Zeichnung erst löschen
		deleteDistrictDrawing();
	}

	var districtDrawing = new google.maps.Polygon({
		clickable: false,
		paths: lineCoordinates,
		strokeColor: color,
		strokeOpacity: 0.8,
		strokeWeight: 2,
		fillColor: color,
		fillOpacity: 0.35
	});
	
	districtDrawing.setMap(map);
	
	districtDrawings.push(districtDrawing);
}

function drawDistrictById() {
	var lineCoordinates = [];
	var color;

	for (var district in districtsMap) {
		// Punkt hinzufuegen
		lineCoordinates.push(districtsMap[district].anchor);
		color = districtsMap[district].color;
    }
	
	drawDistrict(lineCoordinates, color);
}

function drawAllDistricts(init) {
	deleteDistrictDrawing();

	var lineCoordinates = [];
	var index = 0;
	var id = districtsMap[0].id;
	var color = districtsMap[0].color;

	for (var district in districtsMap) {

		// Revier anzeigen
		if (id != districtsMap[district].id)
		{
			if (init) {
				displayedDistricts[id] = true;
			}
		
			if (displayedDistricts[id]) {
				drawDistrict(lineCoordinates, color);
			}
			
			id = districtsMap[district].id;
			index = 0;
			lineCoordinates = [];
			color = districtsMap[district].color;
		}

		// Punkt hinzufuegen
		lineCoordinates[index++] = districtsMap[district].anchor;
	}

	if (init) {
		displayedDistricts[id] = true;
	}
	
	if (displayedDistricts[id]) {
		drawDistrict(lineCoordinates, color);
	}
}

function onMapClickModeWhereabouts(location) {
	if (opMode == 0 && districtID > 0) {
		markers.push(new google.maps.Marker({
		  clickable: true,
		  position: location, 
		  map: map,
		  icon: markerIcon
		}));
	}
}

function onMapClickModeDistricts(location) {
	if (districtID > 0 && opMode == 0) {
		markers.push(new google.maps.Marker({
			position: location, 
			map: map
		}));
			
		if (markers.length == 3 || markers.length == 1) {
			// Bei mindestens drei Punkten kann gespeichert werden
			// Bei mindestens einem kann Rückgängig gemacht werden
			addTextDisplay();
		}
	}
}

function onApplyClickModeWhereabouts() {
	if (districtID > 0 && typeToDraw > 0) {
		// Koordinaten uebertragen
		coordinates = "";
		
		// Save
		if (opMode == 0 && markers.length > 0)
		{
			for (var i = 0; i < markers.length; i++) {
				coordinates += "&lat" + i + "=" + markers[i].position.lat();
				coordinates += "&lng" + i + "=" + markers[i].position.lng();
			}
			saveWhereabouts("save=1&typeid=" + typeToDraw + "&districtid=" + districtID, coordinates);
		}
		// Delete
		else if (opMode == 1 && toDelete.length > 0)
		{
			for (var i = 0; i < toDelete.length; i++) {
				coordinates += "&wid" + i + "=" + toDelete[i];
			}
			saveWhereabouts("delete=1", coordinates);
		}
	}
}

function onApplyClickModeDistricts() {
	if (districtID > 0 && opMode == 0 && markers.length > 2) {	
		coordinates = "";
		
		for (var i = 0; i < markers.length; i++) {
			coordinates += "&lat" + i + "=" + markers[i].position.lat();
			coordinates += "&lng" + i + "=" + markers[i].position.lng();
		}
		saveDistrict("districtid=" + districtID + "&save=1", coordinates);
		onDistrictChanged();
	}
}

/*
 * Nur zum Orte bearbeiten
 */
function addInsertButton() {
  var controlDiv = document.createElement('div');
  var control = new InsertControl(controlDiv);

  controlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(controlDiv);
}

function addDeleteButton() {
  var controlDiv = document.createElement('div');
  var control = new DeleteControl(controlDiv);

  controlDiv.index = 2;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(controlDiv);
}
// -----------------------------------------------------------------------------

/*
 * Nur zum Reviere bearbeiten
 */
function addNewBordersButton() {
  var controlDiv = document.createElement('div');
  var control = new NewBordersControl(controlDiv);

  controlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(controlDiv);
}

function addUndoButton() {
  var controlDiv = document.createElement('div');
  var control = new UndoControl(controlDiv);

  controlDiv.index = 2;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(controlDiv);
}
// -----------------------------------------------------------------------------

function addApplyButton() {
  // Create the DIV to hold the control and
  // call the HomeControl() constructor passing
  // in this DIV.
  var controlDiv = document.createElement('div');
  var control = new ApplyControl(controlDiv);

  controlDiv.index = 3;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(controlDiv);
}

function addTextDisplay() {
  map.controls[google.maps.ControlPosition.BOTTOM_CENTER].pop();
  
  var controlDiv = document.createElement('div');
  var control = new TextControl(controlDiv);

  controlDiv.index = 4;
  map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(controlDiv);
}

function createControl(controlDiv, bgcolor, title) {
	// Set CSS styles for the DIV containing the control
	// Setting padding to 5 px will offset the control
	// from the edge of the map
	controlDiv.style.padding = '5px';
	
	// Set CSS for the control border
	var controlUI = document.createElement('div');
	controlUI.style.backgroundColor = bgcolor;
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderWidth = '2px';
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlUI.title = title;
	controlDiv.appendChild(controlUI);
	
	// Set CSS for the control interior
	var controlText = document.createElement('div');
	controlText.style.fontFamily = 'Arial,sans-serif';
	controlText.style.fontSize = '20px';
	controlText.style.paddingLeft = '4px';
	controlText.style.paddingRight = '4px';
	controlText.innerHTML = '<b>' + title + '</b>';
	controlUI.appendChild(controlText);
	
	return controlUI;
}

/*
 * Nur zum Orte bearbeiten
 */
function InsertControl(controlDiv) {
	var controlUI = createControl(controlDiv, 'green', 'Neue Orte eintragen');
  
	// Setup the click event listeners
	google.maps.event.addDomListener(controlUI, 'click', function() {
		if (opMode != 0 && districtID > 0) {
			opMode = 0;
			
			drawWhereaboutById();
			addTextDisplay();
		}
	});
}

function DeleteControl(controlDiv) {
	var controlUI = createControl(controlDiv, 'orange', 'Orte entfernen');

	// Setup the click event listeners
	google.maps.event.addDomListener(controlUI, 'click', function() {
		if (opMode != 1 && districtID > 0) {
			opMode = 1;
			
			drawWhereaboutById();	
			addTextDisplay();
		}
	});
}
// -----------------------------------------------------------------------------

/*
 * Nur zum Reviere bearbeiten
 */
function NewBordersControl(controlDiv) {
	var controlUI = createControl(controlDiv, 'green', 'Revier neu abstecken');

	// Setup the click event listeners
	google.maps.event.addDomListener(controlUI, 'click', function() {
		if (opMode != 0 && districtID > 0) {
			opMode = 0;
		
			deleteMarkers();
			deleteDistrictDrawing();
			addTextDisplay();
		}
	});
}

function UndoControl(controlDiv) {
	var controlUI = createControl(controlDiv, 'orange', 'Rückgängig');

	// Setup the click event listeners
	google.maps.event.addDomListener(controlUI, 'click', function() {
		if (districtID > 0 && markers.length > 0) {
			deleteLastMarker();

			if (markers.length == 0 || markers.length == 2) {
				addTextDisplay();
			}
		}
	});
}
// -----------------------------------------------------------------------------

function ApplyControl(controlDiv) {
	var controlUI = createControl(controlDiv, 'LightBlue', 'Speichern');

	// Setup the click event listeners
	switch(editMode) {
	case 0:	// Orte bearbeiten
		google.maps.event.addDomListener(controlUI, 'click', function() {
			onApplyClickModeWhereabouts();
		});
		break;
		
	case 1:	// Reviere bearbeiten
		google.maps.event.addDomListener(controlUI, 'click', function() {
			onApplyClickModeDistricts();
		});
		break;
	}
}

function TextControl(controlDiv, map) {
	var text = "";
	var color = "";
	
	if (districtID <= 0) {
		// Revier muss erst ausgewählt werden
		text = "Bitte erst J&auml;ger und Revier ausw&auml;hlen";
		color = "red";
	} else if (editMode == 0) { // Orte bearbeiten
		switch(opMode) {
			case 0:
				text = "Sie befinden sich im Einf&uuml;gemodus.<br />"
					+ "Neue Orte durch Klick auf Karte hinzuf&uuml;gen.<br />"
					+ "&Auml;nderungen mit <b>Speichern</b> &uuml;bernehmen.";
				color = "green";
				break;
			case 1:
				text = "Sie befinden sich im L&ouml;schmodus.<br />"
					+ "Vorhandene Orte durch Klick auf die Markierung entfernen.<br />"
					+ "&Auml;nderungen mit <b>Speichern</b> &uuml;bernehmen.";
				color = "orange";
				break;
			default:
				text = "Bitte erst die gew&uuml;nschte Operation ausw&auml;hlen:<br />"
					+ "<b>Neue Orte eintragen</b> oder <b>Orte l&ouml;schen</b>";
				color = "red";
				break;
		}
	} else if (editMode == 1) {	// Revier bearbeiten
		switch(opMode) {
			case 0:
				text = "Eckpunkte des Reviers durch Klick auf Karte hinzuf&uuml;gen.<br />"
					+ "Die Eckpunkte m&uuml;ssen in der <b>richtigen Reihenfolge</b> angegeben werden!<br />";
					
				if (markers.length > 0) {
					text += "Zuletzt gesetzten Eckpunkt mit <b>R&uuml;ckg&auml;ngig</b> wieder entfernen.<br />";
				}
				
				if (markers.length >= 3) {
					text += "&Auml;nderungen mit <b>Speichern</b> &uuml;bernehmen.";
					color = "blue";
				} else {
					text += "Mindestens drei Eckpunkte des Reviers erforderlich.";
					color = "green";
				}
			break;
			
			default:
				text = "Um das Revier neu abzustecken,<br />"
						+ "bitte auf <b>Revier neu abstecken</b> klicken.";
				color = "red";
				break;
		}
	}
	
	controlDiv.style.padding = '20px';

	// Set CSS for the control border
	var controlUI = document.createElement('div');
	controlUI.style.backgroundColor = 'white';
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderWidth = '2px';
	controlUI.style.borderColor = color;
	controlUI.style.cursor = 'hand';
	controlUI.style.textAlign = 'center';
	controlDiv.appendChild(controlUI);

	// Set CSS for the control interior
	var controlText = document.createElement('div');
	controlText.style.color = color;
	controlText.style.fontFamily = 'Arial,sans-serif';
	controlText.style.fontSize = '20px';
	controlText.style.paddingLeft = '4px';
	controlText.style.paddingRight = '4px';
	controlText.innerHTML = '<b>' + text + '</b>';
	controlUI.appendChild(controlText);
}

function deleteMarkers() {
	for (var i = 0; i < markers.length; i++ ) {
		markers[i].setMap(null);
    }
	
	markers = [];
	toDelete = [];
}

function deleteLastMarker() {
	if (markers.length > 0) {
		markers[markers.length - 1].setMap(null);
		markers.pop();
	}
}

function deleteWhereaboutDrawings() {
	for (var i = 0; i < whereaboutDrawings.length; i++ ) {
		whereaboutDrawings[i].setMap(null);
	}
	whereaboutDrawings = [];
}

function getWhereabouts() {
	if (editMode == 0 && districtID > 0 && typeToDraw > 0) {
		whereaboutsMap = {};
		
		// Ladeanimation einblenden
		showLoadAnim();
		
		$.ajax({
			type: "GET",
			url: "ajax/getWhereabouts.js.php",
			async: false,
			data: {	districtid: districtID,
					typeid: typeToDraw },
			dataType: "script",
			success: function(data, textStatus){
				drawWhereaboutById();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("Daten konnten nicht geladen werden!\nFehlermeldung:\n" + textStatus);
			}
		});
		
		// Ladeanimation ausblenden
		hideLoadAnim();
	}
}

function getDistrict() {
	if (districtID > 0) {
		districtsMap = {};
		
		// Ladeanimation einblenden
		showLoadAnim();
		
		$.ajax({
			type: "GET",
			url: "ajax/getDistrictAnchors.js.php",
			async: false,
			data: { districtid: districtID },
			dataType: "script",
			success: function(data, textStatus){
				drawDistrictById();
				getWhereabouts();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("Daten konnten nicht geladen werden!\nFehlermeldung:\n" + textStatus);
			}
		});
		
		// Ladeanimation ausblenden
		hideLoadAnim();
	}
}

function getAllDistricts() {
	districtsMap = {};
		
	// Ladeanimation einblenden
	showLoadAnim();
	
	$.ajax({
		type: "GET",
		url: "ajax/getDistrictAnchors.js.php",
		async: false,
		dataType: "script",
		success: function(data, textStatus){
			drawAllDistricts(true);
			getAllWhereabouts();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert("Daten konnten nicht geladen werden!\nFehlermeldung:\n" + textStatus);
		}
	});
		
	// Ladeanimation ausblenden
	hideLoadAnim();
}

function getAllWhereabouts() {
	whereaboutsMap = {};
		
	// Ladeanimation einblenden
	showLoadAnim();
	
	$.ajax({
		type: "GET",
		url: "ajax/getWhereabouts.js.php",
		async: false,
		dataType: "script",
		success: function(data, textStatus){
			drawAllWhereabouts(true);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert("Daten konnten nicht geladen werden!\nFehlermeldung:\n" + textStatus);
		}
	});
	
	// Ladeanimation ausblenden
	hideLoadAnim();
}

function getDistrictList() {
	if (hunterID > 0) {
		hunterDistricts = {};
		
		// Ladeanimation einblenden
		showLoadAnim();
	
		$.ajax({
			type: "GET",
			url: "ajax/getDistrictList.js.php",
			async: false,
			data: { hunterid: hunterID },
			dataType: "script",
			success: function(data, textStatus) {
				fillDistrictsCbobox();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("Daten konnten nicht geladen werden!\nFehlermeldung:\n" + textStatus);
			}
		});
		
		// Ladeanimation ausblenden
		hideLoadAnim();
	}
}

function saveWhereabouts(operation, coordinates) {
	if (editMode == 0 && districtID > 0 && typeToDraw > 0) {
		// Ladeanimation einblenden
		showLoadAnim();
	
		$.ajax({
			type: "POST",
			url: "ajax/saveWhereabouts.php",
			async: false,
			data: operation + coordinates,
			success: function(data, textStatus){
				getWhereabouts();
				alert(data == "" ? "Daten gespeichert." : "Fehler beim Speichern der Daten!\nFehlermeldung:\n" + data);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("Daten konnten nicht gespeichert werden!\nFehlermeldung:\n" + textStatus);
			}
		});
		
		// Ladeanimation ausblenden
		hideLoadAnim();
	}
}

function saveDistrict(operation, coordinates) {
	if (editMode == 1) {
		// Ladeanimation einblenden
		showLoadAnim();
		ajaxSuccess = false;
		
		$.ajax({
			type: "POST",
			url: "ajax/saveDistrict.php",
			async: false,
			data: operation + coordinates,
			success: function(data, textStatus){
				if (data == "") {
					ajaxSuccess = true;
					alert("Daten gespeichert.");
				} else {
					alert("Fehler beim Speichern der Daten!\nFehlermeldung:\n" + data);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert("Daten konnten nicht gespeichert werden!\nFehlermeldung:\n" + textStatus);
			}
		});
		
		// Ladeanimation ausblenden
		hideLoadAnim();
	}
}

function saveSettings(key, value, isString) {
	<?php if (isset($_SESSION['userid'])): ?>
	$.ajax({
		type: "POST",
		url: "ajax/saveSettings.php",
		data: {	sKey: key,
				sValue: value,
				sIsString: isString },
		success: function(data, textStatus){
			if (data != "") {
				alert("Fehler beim Speichern der Einstellungen!\nFehlermeldung:\n" + data);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert("Einstellungen konnten nicht gespeichert werden!\nFehlermeldung:\n" + textStatus);
		}
	});
	<?php endif; ?>
}

function showLoadAnim() {
	document.getElementById('loadingbg').style.display = 'block';
}

function hideLoadAnim() {
	document.getElementById('loadingbg').style.display = 'none';
}

function fillDistrictsCbobox() {
	document.district.districtid.options.length = 0;
	document.district.districtid.options[0] = 
		new Option("Bitte auswählen", 0, true, true);
	
	for (var district in hunterDistricts) {
		document.district.districtid.options[document.district.districtid.options.length] = 
			new Option(hunterDistricts[district].name, hunterDistricts[district].id, 
				false, false);
	}
}

function onTypeChanged() {
	typeToDraw = document.type.typeid.options[document.type.typeid.selectedIndex].value;
	getWhereabouts();
}

function onDistrictChanged() {
	districtID = 
		document.district.districtid.options[document.district.districtid.selectedIndex].value;
	opMode = -1;
	
	// Zeichnungen löschen
	deleteDistrictDrawing();
	deleteWhereaboutDrawings();
	deleteMarkers();
	
	if (districtID > 0) {
		// Revier anzeigen
		getDistrict();
		if (editMode == 0) {
			// Typauswahl aktivieren, gibts nur bei den Orten
			document.type.typeid.disabled = false;
		} else if (editMode == 1) {
			document.deletedistrict.btnDeletedistrict.disabled = false;
			document.districtname.newdistrictname.disabled = false;
			document.districtname.newdistrictname.value = 
			document.district.districtid.options[document.district.districtid.selectedIndex].text;
		}
	} else {
		if (editMode == 0) {
			// Typauswahl deaktivieren, gibts nur bei den Orten
			document.type.typeid.disabled = true;
		} else if (editMode == 1) {
			document.deletedistrict.btnDeletedistrict.disabled = true;
			document.districtname.newdistrictname.disabled = true;			
			document.districtname.newdistrictname.value = "";
		}
	}
	// TextDisplay aktualisieren
	addTextDisplay();
}

function onHunterChanged() {
	hunterID = document.hunter.hunterid.options[document.hunter.hunterid.selectedIndex].value;
	districtID = 0;
	opMode = -1;
	
	if (hunterID > 0) {
		getDistrictList();
		// Typ- und Revierauswahl aktivieren
		document.district.districtid.disabled = false;
		
		if (editMode == 0) {
			// Gibts nur bei den Orten
			document.type.typeid.disabled = false;
		} else if (editMode == 1) {
			// Gibts nur bei den Revieren
			document.newdistrict.btnNewdistrict.disabled = false;
		}
	} else {
		// Typ- und Revierauswahl deaktivieren
		document.district.districtid.disabled = true;
		
		if (editMode == 0) {
			// Gibts nur bei den Orten
			document.type.typeid.disabled = true;
		} else if (editMode == 1) {
			// Gibts nur bei den Revieren
			document.newdistrict.btnNewdistrict.disabled = true;
		}
	}
	
	if (editMode == 1) {
		document.districtname.newdistrictname.value = "";
		document.deletedistrict.btnDeletedistrict.disabled = true;
		document.districtname.newdistrictname.disabled = true;
	}
	
	// Bei der Revierauswahl "Bitte auswählen" selektieren
	document.district.districtid.selectedIndex = 0;
	// Zeichnungen löschen
	deleteDistrictDrawing();
	deleteWhereaboutDrawings();
	deleteMarkers();
	// TextDisplay aktualisieren
	addTextDisplay();
}

function onNewDistrict() {
	saveDistrict("hunterid=" + hunterID + "&newdistrict=1", "");
	onHunterChanged();
}

function onDeleteDistrict() {
	if (confirm('Revier wirklich löschen?')) {
		saveDistrict("districtid=" + districtID + "&deletedistrict=1", "");
		onHunterChanged();
	}
}

function onRenameDistrict() {
	if (document.districtname.newdistrictname.value != "") {
		saveDistrict("districtid=" + districtID + "&newdistrictname=" + 
			document.districtname.newdistrictname.value, "");
			
		if (ajaxSuccess) {
			document.district.districtid.options[document.district.districtid.selectedIndex].text = 
				document.districtname.newdistrictname.value;
		}
	} else {
		alert("Reviername darf nicht leer sein!");
	}
}

function onToggleWhereaboutType(id) {
	// Alle noch nicht vorhandenen werden als true angenommen
	displayedWhereaboutTypes[id] = !displayedWhereaboutTypes[id];
	
	drawAllWhereabouts();
}

function onToggleDisplayDistrict(id) {
	displayedDistricts[id] = !displayedDistricts[id];
	
	drawAllDistricts();
	drawAllWhereabouts();
}

function initialize() {

	map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
	
	// Listener für die Map Settings registrieren
	google.maps.event.addListener(map, 'zoom_changed', function() {
		saveSettings('mapZoom', map.getZoom(), 0);
	});
	google.maps.event.addListener(map, 'dragend', function() {
		saveSettings('mapCenterLat', map.getCenter().lat(), 0);
		saveSettings('mapCenterLng', map.getCenter().lng(), 0);
	});
	google.maps.event.addListener(map, 'maptypeid_changed', function() {
		saveSettings('mapDisplayType', map.getMapTypeId().toUpperCase(), 1);
	});
	
	// Overlays hinzufügen
	switch (editMode) {
	case 0:	// Orte bearbeiten
		addInsertButton();
		addDeleteButton();
		
		// Listener für Mapclick registrieren
		google.maps.event.addListener(map, 'click', function(event) {
			onMapClickModeWhereabouts(event.latLng);
		});
		break;
		
	case 1:	// Reviere bearbeiten
		addNewBordersButton();
		addUndoButton();
		
		// Listener für Mapclick registrieren
		google.maps.event.addListener(map, 'click', function(event) {
			onMapClickModeDistricts(event.latLng);
		});
		break;
		
	case 2:	// Karte anzeigen
		getAllDistricts();
		break;
	}
	
	if (editMode != 2) {
		addApplyButton();
		addTextDisplay();
	}
	
	if (editMode == -1) {
		alert("Editmode not set!");
	}
	
	// Ladeanimation ausblenden
	hideLoadAnim();
}

google.maps.event.addDomListener(window, 'load', initialize);